package com.weaver.seconddev.api.tl.service;

/**
 * E9原始类路径:com.api.tl.service.DictCommonService
 * JAVA替换说明文档:https://ncnb71xwgeda.feishu.cn/wiki/JLsjwxK8xiHuPIkdu0Dc27Rlnaf?from=from_copylink
 */
import com.alibaba.fastjson.JSON;
import java.util.*;
import java.util.stream.Collectors;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;

/**
 * 字典信息公共查询类
 * @author gz
 * @version V1.0
 * @className: DictCommonService
 * @date 2024/7/17 9:32
 */
@org.springframework.stereotype.Service("tl_service_DictCommonService")
public class DictCommonService {

    public static final org.slf4j.Logger logger_4ef5bd46 = LoggerFactory.getLogger(DictCommonService.class);


    /**
     * 根据字典类型查询字典信息
     * @param dictType 类型
     * @return 字典信息
     */
    public static Map<String, Map<String, String>> getDictInfoByType(String... dictType) {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        // 构建查询条件 逗号分割
        String dictTypeStr = Arrays.stream(dictType).map(s -> "'" + s + "'").collect(Collectors.joining(","));
        // 查询数据库
       // String sql = "SELECT config_type, config_key, config_value FROM uf_develop_config WHERE config_type in (" + dictTypeStr + ");";
        String sql = "SELECT uf_develop_config.config_type AS config_type, uf_develop_config.config_key AS config_key, uf_develop_config.config_value AS config_value\n" +
                "FROM e10_common.uf_develop_config\n" +
                "WHERE uf_develop_config.config_type IN (" + dictTypeStr + ")\n" +
                "\tAND uf_develop_config.delete_type = 0\n" +
                "\tAND uf_develop_config.tenant_key = 'tl2k2r96ty'";
        logger_4ef5bd46.info(String.valueOf("=======find dict info ======= sql: " + sql));
        String poolname = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeQuery(sql, poolname);
        // 获取查询结果
        List<Map<String, String>> maps = new ArrayList<>();
        while (rs.next()) {
            Map<String, String> map = new HashMap<>();
            map.put("type", rs.getString("config_type"));
            map.put("key", rs.getString("config_key"));
            map.put("value", rs.getString("config_value"));
            maps.add(map);
        }
        // 构建返回参数
        Map<String, Map<String, String>> dictInfo = maps.stream().collect(Collectors.groupingBy(map -> map.get("type"), Collectors.toMap(map -> map.get("key"), map -> map.get("value"), (v1, v2) -> v1)));
        logger_4ef5bd46.info("=======find dict info ======= dictInfo: " + JSON.toJSONString(dictInfo));
        return dictInfo;
    }

    /**
     * @param dictKey 字典key
     * @return 字典value
     */
    public static Map<String, String> getValueByKey(String... dictKey) {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        // 构建查询条件 逗号分割
        String dictKeyStr = Arrays.stream(dictKey).map(s -> "'" + s + "'").collect(Collectors.joining(","));
        // 查询数据库
        //String sql = "SELECT config_key, config_value FROM uf_develop_config WHERE config_key in (" + dictKeyStr + ")";
        String sql = "SELECT uf_develop_config.config_key AS config_key, uf_develop_config.config_value AS config_value\n" +
                "FROM e10_common.uf_develop_config\n" +
                "WHERE uf_develop_config.config_key IN (" + dictKeyStr + ")\n" +
                "\tAND uf_develop_config.delete_type = 0\n" +
                "\tAND uf_develop_config.tenant_key = 'tl2k2r96ty'";
        logger_4ef5bd46.info(String.valueOf("=======find dict value info ======= sql: " + sql));
        String poolname_og = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeQuery(sql, poolname_og);
        // 获取查询结果
        Map<String, String> dictInfo = new HashMap<>(dictKey.length);
        while (rs.next()) {
            dictInfo.put(rs.getString("config_key"), rs.getString("config_value"));
        }
        logger_4ef5bd46.info("=======find dict value info ======= dictInfo: " + JSON.toJSONString(dictInfo));
        // 返回结果
        return dictInfo;
    }

    /**
     * 根据字典类型及字典key查询字典信息
     * @param dictKey 字典key
     * @return 字典value
     */
    public static String getValue(String dictType, String dictKey) {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        // 查询数据库
        //String sql = "SELECT config_key, config_value FROM uf_develop_config WHERE config_type = ? and config_key = ?";
        String sql = "SELECT uf_develop_config.config_key AS config_key, uf_develop_config.config_value AS config_value\n" +
                "FROM e10_common.uf_develop_config\n" +
                "WHERE uf_develop_config.config_type = ?\n" +
                "\tAND uf_develop_config.config_key = ?\n" +
                "\tAND uf_develop_config.delete_type = 0\n" +
                "\tAND uf_develop_config.tenant_key = 'tl2k2r96ty'";
        logger_4ef5bd46.info(String.valueOf("=======find dict value info ======= sql: " + sql + " " + dictType + " " + dictKey));
        String poolname_di = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        rs.executeQuery(sql, poolname_di, dictType, dictKey);
        // 获取查询结果
        String configValue = "";
        if (rs.next()) {
            configValue = rs.getString("config_value");
        }
        logger_4ef5bd46.info(String.valueOf("=======find dict value info ======= configValue: " + configValue));
        // 返回结果
        return configValue;
    }

    public static <T> T get(T t)  {
        if (t == null) {
            throw null;
        }
        return t;
    }
}
